<template>
  <div style="height: 100%;">
    <a-row v-for="(row, index) in rows" :key="index" :style="{paddingTop: index > 0 ? '6px' : '', height: getRowHeight(rows, row)}">
      <a-col v-for="(col, index) in row.cols" :key="index" :span="col.span"
             :style="{paddingLeft: index > 0 ? '6px' : '', height: '100%'}">
        <template v-if="col.rows && col.rows.length > 0">
          <view-redirect-row :rows="col.rows"
                             :relationList="relationList"
                             :refreshMap="refreshMap"
                             :isShowInTab="isShowInTab"
                             :subViews="subViews"
                             :stores="stores"
                             :parameterValue="parameterValue"
                             :formOption="formOption"
                             :bpmOption="bpmOption"
                             v-on="$listeners"
          ></view-redirect-row>
        </template>
        <template v-else-if="col.tabs && col.tabs.length > 0">
          <a-card :bordered="false" :style="col.height ? {height: col.height + 'px', overflow: 'auto'} : {height: '100%', overflow: 'auto'}">
            <a-tabs>
              <a-tab-pane :tab="$locale(tab.viewItem.viewName)" :key="index+1" v-for="(tab, index) in col.tabs"
                          v-if="tab.viewItem.showRules !== 'showAfterFks' || tab.viewItem.showRulesResult">

                <ReportWidgetRedirect v-if="tab.viewItem.viewType === 'report'"
                                      :widgetCode="tab.viewItem.viewCode"
                                      :relationList="relationList"
                                      :refreshMap="refreshMap"
                                      :stores="stores"
                                      :parameterValue="parameterValue"
                                      :tab-config="tab.viewItem"
                                      :isShowInTab="isShowInTab"
                                      v-on="$listeners">
                </ReportWidgetRedirect>

                <TreeRedirect v-else-if="tab.viewItem.viewType === 'tree'"
                              :viewCode="tab.viewItem.viewCode"
                              :subViews="subViews"
                              :relationList="relationList"
                              :refreshMap="refreshMap"
                              :stores="stores"
                              :tab-config="tab.viewItem"
                              :isShowInTab="isShowInTab"
                              :relationStatus="tab.viewItem.relationStatus"
                              :relationOpenFks="tab.viewItem.relationOpenFks"
                              v-on="$listeners">
                </TreeRedirect>

                <BpmForm v-else-if="tab.viewItem.viewType === 'form' && bpmOption && tab.viewItem.flowForm === 'Y'"
                         :formCode="tab.viewItem.viewCode"
                         :layoutCode="bpmOption.formCode"
                         :relationStatus="tab.viewItem.relationStatus"
                         :relationOpenFks="tab.viewItem.relationOpenFks"
                         :relationList="relationList"
                         :stores="stores"
                         :tab-config="tab.viewItem"
                         :showCloseBut="bpmOption.showCloseBut"
                         :bpmId="bpmOption.bpmId"
                         :bpmIdType="bpmOption.bpmIdType"
                         :processInstanceId="bpmOption.processInstanceId"
                         :processDefId="bpmOption.processDefId"
                         :formId="bpmOption.formId"
                         :fks="bpmOption.fks"
                         :parentValue="bpmOption.parentValue"
                         :buttonDatas="bpmOption.buttonDatas"
                         :getBpmOperateVoUrl="bpmOption.getBpmOperateVoUrl"
                         :processInstanceName="bpmOption.processInstanceName"
                         v-on="$listeners"
                         v-bind="$attrs"/>

                <form-redirect v-else-if="tab.viewItem.viewType === 'form'"
                               :formCode="tab.viewItem.viewCode"
                               :relationList="relationList"
                               :refreshMap="refreshMap"
                               :relationStatus="tab.viewItem.relationStatus"
                               :relationOpenFks="tab.viewItem.relationOpenFks"
                               :hiddenSaveBut="tab.viewItem.saveBut === 'display'"
                               :func="tab.viewItem.formStatus === 'auto' ? ((tab.viewItem.mainForm !== 'N' && formOption) ? formOption.func : 'add') : (tab.viewItem.formStatus === 'disabled' ? 'detail' : 'edit')"
                               :stores="stores"
                               :tab-config="tab.viewItem"
                               :isShowInTab="isShowInTab"
                               :formId="(tab.viewItem.mainForm !== 'N' && formOption) ? formOption.formId : undefined"
                               :fks="(tab.viewItem.mainForm !== 'N' && formOption) ? formOption.fks : undefined"
                               :parentValue="(tab.viewItem.mainForm !== 'N' && formOption) ? formOption.parentValue : undefined"
                               :saveInPage="(tab.viewItem.mainForm !== 'N' && formOption) ? formOption.saveInPage : undefined"
                               :disQueryDB="(tab.viewItem.mainForm !== 'N' && formOption) ? formOption.disQueryDB : undefined"
                               :record="(tab.viewItem.mainForm !== 'N' && formOption) ? formOption.record : undefined"
                               :isTab="formOption ? formOption.isTab : undefined"
                               @close="$emit('closeFromView')"
                               @hideDialogButtons="$emit('hideDialogButtonsFromView')"
                               @addParentSpinning="$emit('addParentSpinningFromView')"
                               @delParentSpinning="$emit('delParentSpinningFromView')"
                               @handleOk="handleOkFromView"
                               @setFormTitle="setFormTitleFromView"
                               v-on="$listeners">
                </form-redirect>

                <view-redirect v-else-if="tab.viewItem.viewType === 'table'"
                               :viewCode="tab.viewItem.viewCode"
                               :relationList="relationList"
                               :refreshMap="refreshMap"
                               :parameterValue="parameterValue"
                               :isShowInTab="isShowInTab"
                               :subViews="subViews"
                               :stores="stores"
                               :tab-config="tab.viewItem"
                               :relationStatus="tab.viewItem.relationStatus"
                               :relationOpenFks="tab.viewItem.relationOpenFks"
                               v-on="$listeners" >
                </view-redirect>

                <view-editable-redirect v-else-if="tab.viewItem.viewType === 'editableTable'"
                               :viewCode="tab.viewItem.viewCode"
                               :relationList="relationList"
                               :refreshMap="refreshMap"
                               :parameterValue="parameterValue"
                               :isShowInTab="isShowInTab"
                               :subViews="subViews"
                               :stores="stores"
                               :tab-config="tab.viewItem"
                               :relationStatus="tab.viewItem.relationStatus"
                               :relationOpenFks="tab.viewItem.relationOpenFks"
                               v-on="$listeners" >
                </view-editable-redirect>

              </a-tab-pane>
            </a-tabs>
          </a-card>
        </template>
        <a-card v-else :bordered="false" :style="col.height ? {height: col.height + 'px', overflow: 'auto'} : {height: '100%', overflow: 'auto'}">

          <ReportWidgetRedirect v-if="col.viewItem.viewType === 'report'"
                                :widgetCode="col.viewItem.viewCode"
                                :relationList="relationList"
                                :refreshMap="refreshMap"
                                :stores="stores"
                                :parameterValue="parameterValue"
                                :isShowInTab="isShowInTab"
                                v-on="$listeners">
          </ReportWidgetRedirect>

          <TreeRedirect v-else-if="col.viewItem.viewType === 'tree'"
                        v-show="col.viewItem.showRules !== 'showAfterFks' || col.viewItem.showRulesResult"
                        :viewCode="col.viewItem.viewCode"
                        :subViews="subViews"
                        :relationList="relationList"
                        :refreshMap="refreshMap"
                        :isShowInTab="isShowInTab"
                        :stores="stores"
                        :relationStatus="col.viewItem.relationStatus"
                        :relationOpenFks="col.viewItem.relationOpenFks"
                        v-on="$listeners" >
          </TreeRedirect>

          <BpmForm v-else-if="col.viewItem.viewType === 'form' && bpmOption && col.viewItem.flowForm === 'Y'"
                   :formCode="col.viewItem.viewCode"
                   :layoutCode="bpmOption.formCode"
                   :relationStatus="col.viewItem.relationStatus"
                   :relationOpenFks="col.viewItem.relationOpenFks"
                   :relationList="relationList"
                   :stores="stores"
                   :showCloseBut="bpmOption.showCloseBut"
                   :bpmId="bpmOption.bpmId"
                   :bpmIdType="bpmOption.bpmIdType"
                   :processInstanceId="bpmOption.processInstanceId"
                   :processDefId="bpmOption.processDefId"
                   :formId="bpmOption.formId"
                   :fks="bpmOption.fks"
                   :parentValue="bpmOption.parentValue"
                   :buttonDatas="bpmOption.buttonDatas"
                   :getBpmOperateVoUrl="bpmOption.getBpmOperateVoUrl"
                   :processInstanceName="bpmOption.processInstanceName"
                   v-on="$listeners"
                   v-bind="$attrs"/>

          <form-redirect v-else-if="col.viewItem.viewType === 'form'"
                         :formCode="col.viewItem.viewCode"
                         :relationList="relationList"
                         :refreshMap="refreshMap"
                         :relationStatus="col.viewItem.relationStatus"
                         :relationOpenFks="col.viewItem.relationOpenFks"
                         :hiddenSaveBut="col.viewItem.saveBut === 'display'"
                         :func="col.viewItem.formStatus === 'auto' ? ((col.viewItem.mainForm !== 'N' && formOption) ? formOption.func : 'add') : (col.viewItem.formStatus === 'disabled' ? 'detail' : 'edit')"
                         :stores="stores"
                         :isShowInTab="isShowInTab"
                         :formId="(col.viewItem.mainForm !== 'N' && formOption) ? formOption.formId : undefined"
                         :fks="(col.viewItem.mainForm !== 'N' && formOption) ? formOption.fks : undefined"
                         :parentValue="(col.viewItem.mainForm !== 'N' && formOption) ? formOption.parentValue : undefined"
                         :saveInPage="(col.viewItem.mainForm !== 'N' && formOption) ? formOption.saveInPage : undefined"
                         :disQueryDB="(col.viewItem.mainForm !== 'N' && formOption) ? formOption.disQueryDB : undefined"
                         :record="(col.viewItem.mainForm !== 'N' && formOption) ? formOption.record : undefined"
                         :isTab="formOption ? formOption.isTab : undefined"
                         @close="$emit('closeFromView')"
                         @hideDialogButtons="$emit('hideDialogButtonsFromView')"
                         @addParentSpinning="$emit('addParentSpinningFromView')"
                         @delParentSpinning="$emit('delParentSpinningFromView')"
                         @handleOk="handleOkFromView"
                         @setFormTitle="setFormTitleFromView"
                         v-on="$listeners">
          </form-redirect>

          <view-redirect v-else-if="col.viewItem.viewType === 'table'"
                         v-show="col.viewItem.showRules !== 'showAfterFks' || col.viewItem.showRulesResult"
                         :viewCode="col.viewItem.viewCode"
                         :relationList="relationList"
                         :refreshMap="refreshMap"
                         :parameterValue="parameterValue"
                         :isShowInTab="isShowInTab"
                         :subViews="subViews"
                         :stores="stores"
                         :relationStatus="col.viewItem.relationStatus"
                         :relationOpenFks="col.viewItem.relationOpenFks"
                         v-on="$listeners" >
          </view-redirect>

          <view-editable-redirect v-else-if="col.viewItem.viewType === 'editableTable'"
                         v-show="col.viewItem.showRules !== 'showAfterFks' || col.viewItem.showRulesResult"
                         :viewCode="col.viewItem.viewCode"
                         :relationList="relationList"
                         :refreshMap="refreshMap"
                         :parameterValue="parameterValue"
                         :isShowInTab="isShowInTab"
                         :subViews="subViews"
                         :stores="stores"
                         :relationStatus="col.viewItem.relationStatus"
                         :relationOpenFks="col.viewItem.relationOpenFks"
                         v-on="$listeners" >
          </view-editable-redirect>

        </a-card>
      </a-col>
    </a-row>
  </div>
</template>

<script>

import YchConstant from "@/api/YchConstant";
import ViewRedirect from './ViewRedirect'
import ReportWidgetRedirect from './ReportWidgetRedirect'
import TreeRedirect from './TreeRedirect'
import FormRedirect from "./FormRedirect";
import RelationUtil from "./RelationUtil";
import BpmForm from "./BpmForm";
import ViewEditableRedirect from './ViewEditableRedirect'

export default {
  name: 'ViewRedirectRow',
  props: {
    rows: {
      type: Array,
      default: () => []
    },
    relationList: Array,
    refreshMap: Object,
    isShowInTab: Boolean,
    subViews: Object,
    stores: {
      type: Object,
      default: () => {
        return {}
      }
    },
    parameterValue: {
      type: Array,
      default: () => []
    },
    formOption: Object,
    bpmOption: Object,
  },
  components: {
    ViewRedirect,
    ReportWidgetRedirect,
    TreeRedirect,
    FormRedirect,
    BpmForm,
    ViewEditableRedirect,
  },
  data() {
    return {
      url: {
        getViewModel: YchConstant.PREFIX_EFORM + '/engine/viewEngine/getViewModel',
      },
    }
  },
  methods: {
    getRowHeight(rows, row) {
      if (rows.length > 0) {
        const colHeight = row.cols.find(col => col.height);
        if (colHeight) {
          return "auto"
        }
        return 100 / rows.length + '%';
      } else {
        return "auto";
      }
    },
    setFormTitleFromView(title) {
      this.$emit('setFormTitleFromView', title)
    },
    handleOkFromView($vue,isDraft){
      this.$emit('handleOkFromView', $vue,isDraft)
    },
    relationTable(table) {
      if (table.showRules === 'showAfterFks') {
        let tableName = null
        if (this.subViews && this.subViews[table.viewCode]) {
          const result = this.subViews[table.viewCode]
          tableName = result.tableShow.tableName
        }

        const fks = RelationUtil.getChangedRelation({
          relationList: this.relationList,
          tableName: tableName,
          fks: [],
          relationOpenFks: table.relationOpenFks,
        })
        table.showRulesResult = RelationUtil.checkFks({
          fks: fks,
        })
      }
    },
    relationTree(tree) {
      if (tree.showRules === 'showAfterFks') {
        let tableName = null
        if (this.subViews && this.subViews[tree.viewCode]) {
          const result = this.subViews[tree.viewCode]
          const treeData = result.treeData ? JSON.parse(result.treeData) : {}
          if (treeData.type === '1') {
            //普通模式
            const config = treeData.data || {}
            tableName = config.tableName
          } else {
            if (treeData.data.length > 0) {
              tableName = treeData.data[0].tableName
            }
          }
        }

        const fks = RelationUtil.getChangedRelation({
          relationList: this.relationList,
          tableName: tree.tableName,
          fks: [],
          relationOpenFks: tree.relationOpenFks,
        })
        const b = RelationUtil.checkFks({
          fks: fks,
        })
        tree.showRulesResult = b
      }
    }
  },
  watch: {
    relationList: {
      handler() {
        this.rows.forEach(row => {
          row.cols.forEach(col => {
            if (col.viewItem && (col.viewItem.viewType === 'table' || col.viewItem.viewType === 'editableTable')) {
              this.relationTable(col.viewItem);
            } else if (col.viewItem && col.viewItem.viewType === 'tree') {
              this.relationTree(col.viewItem);
            } else if (col.tabs) {
              col.tabs.forEach(tab => {
                if (tab.viewItem.viewType === 'table' || tab.viewItem.viewType === 'editableTable') {
                  this.relationTable(tab.viewItem);
                } else if (tab.viewItem.viewType === 'tree') {
                  this.relationTree(tab.viewItem);
                }
              })
            }
          })
        })
      },
      immediate: true
    },
  },
}
</script>
